﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_Admin_Admin_XemThongTinDonHang : System.Web.UI.Page
{
    int RowPerPage;
    int startIndex;
    int page;
    GlobalFunctions gf;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["page"] != null)
            page = int.Parse(Request["page"]);
        else
            page = 0;

        RowPerPage = int.Parse(ConfigurationSettings.AppSettings["RowPerPage"]);
        startIndex = (page * RowPerPage);

        int gomnhom = 0;
        if (Session["GomNhom"] != null)
            gomnhom = int.Parse(Session["GomNhom"].ToString());
        if (!this.IsPostBack)
        {
            ddlSapXep.SelectedValue = gomnhom.ToString();
        }
        DataDSTableAdapters.DATHANGTableAdapter dhadapter = new DataDSTableAdapters.DATHANGTableAdapter();
        PageInterface1.currentPageName = GlobalFunctions.GetCurrentPageName();
        PageInterface1.currentPageNumber = page;
        PageInterface1.totalNumberOfPage = (int)dhadapter.DATHANG_LayTongSoDonHang(gomnhom) / RowPerPage;
        if ((int)dhadapter.DATHANG_LayTongSoDonHang(gomnhom) % RowPerPage != 0)
            PageInterface1.totalNumberOfPage++;
        gf = new GlobalFunctions();
        if (!this.IsPostBack)
        {
            DataList_DanhSachSanPham.DataSource = dhadapter.DATHANG_LayDonHang(startIndex, RowPerPage,1,gomnhom);
            DataList_DanhSachSanPham.DataBind();
        }
    }
    protected void ddlSapXep_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["GomNhom"] = ddlSapXep.SelectedValue;
        Response.Redirect(GlobalFunctions.GetCurrentPageName());
    }
    protected void DataList_DanhSachSanPham_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataListItem item = e.Item;
            DateTime thoigiandathang = (DateTime)((DataRowView)item.DataItem).Row["ThoiGianDatHang"];
            string thoigian = thoigiandathang.ToString("dd/M/yyyy HH:mm:s");
            Label lblthoigiandathang = (Label)item.FindControl("lblThoiGianDatHang");
            lblthoigiandathang.Text = thoigian;

            Label lblthoigiangiaohang = (Label)item.FindControl("lblThoiGianGiaoHang");
            Object ob = ((DataRowView)item.DataItem).Row["NgayDaGiaoHang"];
            if (ob.ToString() == "")
            {
                int trangthai1 = (int)((DataRowView)item.DataItem).Row["TrangThai"];
                switch (trangthai1)
                {
                    case 1:
                        Object obthoigianhuy = ((DataRowView)item.DataItem).Row["ThoiGianHuy"];
                        if (obthoigianhuy.ToString() != "")
                        {
                            DateTime thhuy = (DateTime)obthoigianhuy;
                            lblthoigiangiaohang.Text = "Đã hủy lúc: " + thhuy.ToString("dd/M/yyyy HH:mm:s");
                        }
                        else
                        {
                            lblthoigiangiaohang.Text = "Đã hủy";
                        }
                        break;
                    case 2:
                        lblthoigiangiaohang.Text = "Đang chờ giao";
                        break;
                    case 4:
                        lblthoigiangiaohang.Text = "Chưa giao";
                        break;
                }
                
            }
            else
            {
                DateTime thoigiangiaohang = (DateTime)ob;
                lblthoigiangiaohang.Text = thoigiangiaohang.ToString("dd/M/yyyy HH:mm:s");
            }
            Label lbltongtien = (Label)item.FindControl("lblTongTien");
            lbltongtien.Text = ((DataRowView)item.DataItem).Row["TongTien"].ToString() + " VND";

            Label lbltrangthai = (Label)item.FindControl("lblTrangThai");
            int trangthai = int.Parse(((DataRowView)item.DataItem).Row["TrangThai"].ToString());
            Label lblhuy = (Label)item.FindControl("lblHuy");
            string iddonhang=((DataRowView)item.DataItem).Row["Id"].ToString();
            int idnhanvien=0;
            switch (trangthai)
            {
                case 1:
                    lbltrangthai.Text = "<select name='" + iddonhang + "radio'><option value='1' selected >Hủy</option><option value='4'>Chưa Giao</option><option value='2'>Đang Chờ Giao</option><option value='3'>Đã Giao</option></select>";
                    lblhuy.Text = "<input type='checkbox' name='chkCapNhat' value='" + ((DataRowView)item.DataItem).Row["Id"].ToString() + "' >Cập Nhật</input>";
                    break;
                case 2:
                    lbltrangthai.Text = "<select name='" + iddonhang + "radio'><option value='1' >Hủy</option><option value='4'>Chưa Giao</option><option value='2' selected >Đang Chờ Giao</option><option value='3'>Đã Giao</option></select>";
                    lblhuy.Text = "<input type='checkbox' name='chkCapNhat' value='" + ((DataRowView)item.DataItem).Row["Id"].ToString() + "' >Cập Nhật</input>";
                    break;
                case 3:
                    lbltrangthai.Text = "<select name='" + iddonhang + "radio'><option value='1'>Hủy</option><option value='4'>Chưa Giao</option><option value='2'>Đang Chờ Giao</option><option value='3' selected >Đã Giao</option></select>";
                    lblhuy.Text = "<input type='checkbox' name='chkCapNhat' value='" + ((DataRowView)item.DataItem).Row["Id"].ToString() + "' >Cập Nhật</input>";
                    idnhanvien=(int)((DataRowView)item.DataItem).Row["Id_NhanVienGiaoHang"];
                    break;
                case 4:
                    lbltrangthai.Text = "<select name='" + iddonhang + "radio'><option value='1'>Hủy</option><option value='4' selected >Chưa Giao</option><option value='2'>Đang Chờ Giao</option><option value='3'>Đã Giao</option></select>";
                    lblhuy.Text = "<input type='checkbox' name='chkCapNhat' value='" + ((DataRowView)item.DataItem).Row["Id"].ToString() + "' >Cập Nhật</input>";
                    break;
                default:
                    lbltrangthai.Text = "Ko biết";
                    lblhuy.Text = "";
                    break;
            }
            DataDSTableAdapters.NHANVIENTableAdapter nvadapter = new DataDSTableAdapters.NHANVIENTableAdapter();
            DataDS.NHANVIENDataTable nvtable = nvadapter.NHANVIEN_LayDanhSach();
            string chuoinhanvien = "";
            chuoinhanvien += "<select name='"+iddonhang+"nhanvien'>";
            chuoinhanvien += "<option value='0'>---</option>";
            for (int i = 0; i < nvtable.Rows.Count; i++)
            {
                if(int.Parse(nvtable.Rows[i]["Id"].ToString())==idnhanvien)
                    chuoinhanvien += "<option value='" + nvtable.Rows[i]["Id"].ToString() + "' selected >" + nvtable.Rows[i]["TenNhanVien"].ToString() + "</option>";
                else
                    chuoinhanvien += "<option value='" + nvtable.Rows[i]["Id"].ToString() + "' >" + nvtable.Rows[i]["TenNhanVien"].ToString() + "</option>";
            }
            chuoinhanvien += "</select>";
            Label lblnhanvien = (Label)item.FindControl("lblNhanVien");
            lblnhanvien.Text = chuoinhanvien;
        }
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        int i;
        /*for (i = 0; i < DataList_DanhSachSanPham.Items.Count; i++)
        {
            DataListItem item= DataList_DanhSachSanPham.Items[i];
            string iddonhang = ((DataRowView)item.DataItem).Row["Id"].ToString();
            object capnhat = Request.Form.Get(iddonhang + "chkCapNhat");
            if (capnhat != null && (Boolean)capnhat == true)
            {
                Response.Write(iddonhang + " ");
            }
        }*/
        string[] capnhat = Request.Form.GetValues("chkCapNhat");
        if (capnhat != null)
        {
            DataDSTableAdapters.DATHANGTableAdapter dhadapter = new DataDSTableAdapters.DATHANGTableAdapter();
            for (i = 0; i < capnhat.Length; i++)
            {
                string iddonhang = capnhat[i];
                string trangthai = Request.Form.Get(iddonhang + "radio");
                //Response.Write(trangthai);
                if (int.Parse(trangthai) == 3)
                {
                    string idnhanvien = Request.Form.Get(iddonhang + "nhanvien");
                    if (int.Parse(idnhanvien) != 0)
                    {
                        dhadapter.DATHANG_CapNhatDonHang(int.Parse(iddonhang), DateTime.Now, int.Parse(idnhanvien), int.Parse(trangthai),null);
                    }

                }
                else
                {
                    if (int.Parse(trangthai) == 2)
                    {
                        dhadapter.DATHANG_CapNhatDonHang(int.Parse(iddonhang), null, null, int.Parse(trangthai), null);
                    }
                    else
                    {
                        if (int.Parse(trangthai) == 1)
                        {
                            dhadapter.DATHANG_CapNhatDonHang(int.Parse(iddonhang), null, null, int.Parse(trangthai), DateTime.Now);
                        }
                        else
                        {
                            dhadapter.DATHANG_CapNhatDonHang(int.Parse(iddonhang), null, null, int.Parse(trangthai), null);
                        }
                    }
                }
            }
        }
        Response.Redirect(Request.UrlReferrer.ToString());
    }
}
